#!/usr/bin/env python2.5
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#



import os
import wsgiref.handlers
import urllib
import sys 
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch
from django.utils import simplejson

from google.appengine.ext import webapp


def RPXTokenHandler(request):
 sys.stderr.write('rpxtoken handler called')
# token = request.get('token')
 token = request.GET.__getitem__('token')
 url = 'https://rpxnow.com/api/v2/auth_info'
 args = {
      'format': 'json',
      'apiKey': '0d5c2ce96d57ee08ec1c3de13381d198a02a9eb8',
      'token': token
      }
 r = urlfetch.fetch(url=url,
                       payload=urllib.urlencode(args),
                       method=urlfetch.POST,
                       headers={'Content-Type':'application/x-www-form-urlencoded'}
                       )
 json = simplejson.loads(r.content)
 if json['stat'] == 'ok':    
  rpx_response = repr(json)
 else :
  rpx_response = 'login attempt failed' 
 return(rpx_response)
#   if json['stat'] == 'ok':    
#    unique_identifier = json['profile']['identifier']
#      nickname = json['profile']['preferredUsername']
#     email = json['profile']['email']
#      # log the user in using the unique_identifier
#      # this should your cookies or session you already have implemented
#      self.log_user_in(unique_identifier)    
#      self.redirect('/hidden')
#    else:
#      self.redirect('/error') 


